var express = require('express');
var router = express.Router();
var User = require('../database/redis/userDao');
var topicDao = require('../database/mysql/topicDao');
var interestsDao = require('../database/mysql/interestsDao');
var writeEJS = require('./writeEJS');

router.get('/', async function (req, res, next) {

  //header需要的数据。。
  await writeEJS.asyncWriteHeader(req,res);

  //通过query获得interest的选择id
  //query参数不合法就等于0
  let queryInterestsId = req.query.interests || 0;
  if (isNaN(queryInterestsId)) {
    queryInterestsId = 0;
  }
  if (queryInterestsId < 0) {
    queryInterestsId = 0;
  }

  //获得interests列表并且写入模板引擎
  let interests = await interestsDao.getAllInterests();
  res.locals.interests = interests;
  res.locals.interestsId = queryInterestsId;

  /*--------------人畜分割线------------ */

  //通过query获得topic的页码
  //query参数不合法就等于1
  let queryPath = req.query.path || 1;
  if (isNaN(queryPath)) {
    queryPath = 1;
  }
  if (queryPath < 1) {
    queryPath = 1;
  }

  //获取tobic列表数据并且写入模板引擎
  let SplitTobic = await topicDao.getSplitTobic(queryPath, queryInterestsId);
  res.locals.queryPath = queryPath;
  res.locals.pathNum = SplitTobic.pathNum;
  res.locals.exitsToicNum = SplitTobic.exitsToicNum;
  res.locals.currentPathNum = SplitTobic.currentPathNum;
  res.locals.topic = SplitTobic.topic;

  res.render('index');
});

module.exports = router;